#include "guibase.h"
#include "../qnigaapp.h"

#include <QAction>
#include <QMenu>
#include <QMenuBar>

namespace QnigaUI
{

GUIBase::GUIBase ()
    : QMainWindow(NULL, 0x0)
{
}

GUIBase::~GUIBase()
{
}

void GUIBase::Setup()
{
    setWindowTitle(tr("Qniga"));

    SetupActions();

    SetupMenus();

    SetupToolbar();

    RetranslateUI();

    setGeometry(QnigaApp::Get().GetGUIConfig().GetMainWinGeometry());

    show();
}

void GUIBase::SetupActions()
{
    // File View Nav Bookmarks Tools Help
    // File: Open Recent Exit
    // View: Enc Fullscreen
    // Nav: PrevPage NextPage FirstPage LastPage GotoPage Backward Forward | Contents
    // Bookmarks: Add ...
    // Tools: BookmarkMan Statistics? BookInfo Contents | Preferences
    // Help: About AboutQt Readme Authors ReleaseNotes FAQ Copying

    SetupFileActions();
    SetupViewActions();
    SetupNavActions();
    SetupBookmarksActions();
    SetupToolsActions();
    SetupHelpActions();
}

void GUIBase::SetupFileActions ()
{
    m_actFileOpen = new QAction(this);
//     m_actFileOpen->setIcon(QIcon(":/fileopen.png") );
    connect(m_actFileOpen, SIGNAL(triggered()), this, SLOT(FileOpen()));

//     for (int i = 0; i < m_set->GetMaxRecentFilesNum(); ++i)
//     {
//         recentFileActs << new QAction( this );
//         recentFileActs[ i ]->setVisible( false);
//         connect(recentFileActs[ i ], SIGNAL( triggered() ), this, SLOT( openRecentFile() ));
//     }

    m_actFileExit = new QAction(this);
//     m_actFileExit->setIcon(QIcon(":/fileclose.png") );
    connect(m_actFileExit, SIGNAL(triggered()), SLOT(close()));
}

void GUIBase::SetupViewActions ()
{
    m_actViewSetEnc = new QAction(this);
    connect(m_actViewSetEnc, SIGNAL(triggered()), SLOT(ViewSetEnc()));

    m_actViewFullScreen = new QAction(this);
//     m_actViewFullScreen->setIcon(QIcon(":/fullscreen.png") );
    m_actViewFullScreen->setCheckable(true);
    connect(m_actViewFullScreen, SIGNAL(triggered(bool)), SLOT(ViewFullScreen(bool)));
}

void GUIBase::SetupNavActions ()
{
    m_actNavPrevPage = new QAction(this);
    m_actNavPrevPage->setIcon(QIcon(":/prevpage.png"));
    connect(m_actNavPrevPage, SIGNAL(triggered()), SLOT(navPrevPage()));

    m_actNavNextPage = new QAction(this);
    m_actNavNextPage->setIcon(QIcon(":/nextpage.png"));
    connect(m_actNavNextPage, SIGNAL(triggered()), SLOT(navNextPage()));

    m_actNavFirstPage = new QAction(this);
    connect(m_actNavFirstPage, SIGNAL(triggered()), SLOT(navFirstPage()));

    m_actNavLastPage = new QAction(this);
    connect(m_actNavLastPage, SIGNAL(triggered()), SLOT(navLastPage()));

    m_actNavGoToPage = new QAction(this);
    connect(m_actNavGoToPage, SIGNAL(triggered()), SLOT(navGoToPage()));

    m_actNavBackward = new QAction(this);
    connect(m_actNavBackward, SIGNAL(triggered()), SLOT(navBackward()));

    m_actNavForward = new QAction(this);
    connect(m_actNavForward, SIGNAL(triggered()), SLOT(navForward()));
}

void GUIBase::SetupBookmarksActions ()
{
    m_actBookmarkAdd = new QAction(this);
    connect(m_actBookmarkAdd, SIGNAL(triggered()), SLOT(bookmarkAdd()));
}

void GUIBase::SetupToolsActions ()
{
    m_actToolsPrefs = new QAction(this);
    connect(m_actToolsPrefs, SIGNAL(triggered()), SLOT(toolsPrefs()));

    m_actToolsBookmarks = new QAction(this);
    connect(m_actToolsBookmarks, SIGNAL(triggered()), SLOT(toolsBookmarks()));

    m_actToolsStatistics = new QAction(this);
    connect(m_actToolsStatistics, SIGNAL(triggered()), SLOT(toolsStatisics()));

    m_actToolsBookInfo = new QAction(this);
    connect(m_actToolsBookInfo, SIGNAL(triggered()), SLOT(toolsBookInfo()));

    m_actToolsFB2Contents = new QAction(this);
    connect(m_actToolsFB2Contents, SIGNAL(triggered()), SLOT(toolsFB2Contents()));
}

void GUIBase::SetupHelpActions ()
{
    m_actHelpAbout = new QAction(this);
    connect(m_actHelpAbout, SIGNAL(triggered()), SLOT(helpAbout()));

    m_actHelpAboutQt = new QAction(this);
    connect(m_actHelpAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

    m_actHelpReadme = new QAction(this);
    connect(m_actHelpReadme, SIGNAL(triggered()), SLOT(helpReadme()));

    m_actHelpAuthors = new QAction(this);
    connect(m_actHelpAuthors, SIGNAL(triggered()), SLOT(helpAuthors()));

    m_actHelpChangelog = new QAction(this);
    connect(m_actHelpChangelog, SIGNAL(triggered()), SLOT(helpChangelog()));

    m_actHelpFAQ = new QAction(this);
    connect(m_actHelpFAQ, SIGNAL(triggered()), SLOT(helpFAQ()));

    m_actHelpCopying = new QAction(this);
    connect(m_actHelpCopying, SIGNAL(triggered()), SLOT(helpCopying()));
}

void GUIBase::SetupMenus ()
{
    SetupFileMenu();
    SetupViewMenu();
    SetupBookmarksMenu();
    SetupNavMenu();
    SetupToolsMenu();
    SetupHelpMenu();
}

void GUIBase::SetupFileMenu ()
{
    m_menuFile = menuBar()->addMenu(tr("&File") );
    m_menuFile->setTearOffEnabled( true);
    m_menuFile->addAction(m_actFileOpen);
//     m_separatorAct = m_menuFile->addSeparator();
//
//     for (int i = 0; i < m_set->GetMaxRecentFilesNum(); ++i)
//         m_menuFile->addAction(recentFileActs[ i ]);
//
//     updateRecentFileActions();
    m_menuFile->addSeparator();
    m_menuFile->addAction(m_actFileExit);
}

void GUIBase::SetupViewMenu ()
{

    m_menuView = menuBar()->addMenu(tr("&View") );
    m_menuView->setTearOffEnabled( true);
    m_menuView->addAction(m_actViewFullScreen);
    m_menuView->addAction(m_actViewSetEnc);
}

void GUIBase::SetupBookmarksMenu ()
{
    m_menuBookmarks = menuBar()->addMenu(tr("&Bookmarks"));
    m_menuBookmarks->setTearOffEnabled(true);
    m_menuBookmarks->addAction(m_actBookmarkAdd);
    m_menuBookmarks->addSeparator();
    m_menuResetBookmark  = m_menuBookmarks->addMenu(tr("Reset To Current Position"));
    m_menuRenameBookmark = m_menuBookmarks->addMenu(tr("Rename Bookmark"));
    m_menuRemoveBookmark = m_menuBookmarks->addMenu(tr("Remove Bookmark"));
    m_menuBookmarks->addSeparator();
    //updateBookmarksActions();
}

void GUIBase::SetupNavMenu ()
{
    m_menuNav = menuBar()->addMenu(tr("&Navigation"));
    m_menuNav->setTearOffEnabled(true);
    m_menuNav->addAction(m_actNavPrevPage);
    m_menuNav->addAction(m_actNavNextPage);
    m_menuNav->addAction(m_actNavFirstPage);
    m_menuNav->addAction(m_actNavLastPage);
    m_menuNav->addAction(m_actNavGoToPage);
    m_menuNav->addSeparator();
    m_menuNav->addAction(m_actNavBackward);
    m_menuNav->addAction(m_actNavForward);
    m_menuNav->addSeparator();
//     m_menuFB2Contents = m_menuNav->addMenu( tr( "FictionBook 2 Contents" ) );
//     updateFB2ContentsActions();
}

void GUIBase::SetupToolsMenu ()
{
    m_menuTools = menuBar()->addMenu(tr("Tools"));
    m_menuTools->setTearOffEnabled(true);
    m_menuTools->addAction(m_actToolsBookmarks);
    m_menuTools->addAction(m_actToolsStatistics);
    m_menuTools->addAction(m_actToolsBookInfo);
    m_menuTools->addAction(m_actToolsFB2Contents);
}

void GUIBase::SetupHelpMenu ()
{
    m_menuHelp = menuBar()->addMenu(tr("&Help"));
    m_menuHelp->setTearOffEnabled(true);
    m_menuHelp->addAction(m_actHelpAbout);
    m_menuHelp->addAction(m_actHelpAboutQt);
    m_menuHelp->addSeparator();
    m_menuHelp->addAction(m_actHelpCopying);
    m_menuHelp->addAction(m_actHelpReadme);
    m_menuHelp->addAction(m_actHelpAuthors);
    m_menuHelp->addAction(m_actHelpChangelog);
    m_menuHelp->addAction(m_actHelpFAQ);
}

void GUIBase::SetupToolbar ()
{

}

void GUIBase::RetranslateUI ()
{
    m_actFileOpen->setText( tr( "Open" ) );
    m_actFileOpen->setShortcut( tr( "Ctrl+O" ) );

//     m_actFileConf->setText( tr( "Configure" ) );

    m_actFileExit->setText( tr( "E&xit" ) );
    m_actFileExit->setShortcut( tr( "Ctrl+Q" ) );

    m_actViewSetEnc->setText( tr( "Set Encoding..." ) );

    m_actViewFullScreen->setText( tr( "&Full Screen Mode" ) );
    m_actViewFullScreen->setShortcut( tr( "F11" ) );

//     m_actViewShowSlider->setText( tr( "Show Slider" ) );
//     m_actViewShowSlider->setShortcut( tr( "F10" ) );
//
//     m_actViewPageAutoChanging->setText( tr( "Automatic Page Changing" ) );
//     m_actViewPageAutoChanging->setShortcut( tr( "F2" ) );

    m_actBookmarkAdd->setText( tr( "Add &Bookmark..." ) );
    m_actBookmarkAdd->setShortcut( tr( "Ctrl+B" ) );

    m_actNavPrevPage->setText( tr( "&Previous page" ) );
    m_actNavPrevPage->setShortcut( Qt::Key_PageUp );

    m_actNavNextPage->setText( tr( "&Next page" ) );
    m_actNavNextPage->setShortcut( Qt::Key_PageDown );

    m_actNavFirstPage->setText( tr( "&First page" ) );
    m_actNavFirstPage->setShortcut( Qt::Key_Home );

    m_actNavLastPage->setText( tr( "&Last page" ) );
    m_actNavLastPage->setShortcut( Qt::Key_End );

    m_actNavGoToPage->setText( tr( "Go to page..." ) );
    m_actNavGoToPage->setShortcut( tr( "Ctrl+G" ) );

    m_actNavBackward->setText( tr( "Back" ) );
    m_actNavBackward->setShortcut( tr( "Alt+Left" ) );

    m_actNavForward->setText( tr( "Forward" ) );
    m_actNavForward->setShortcut( tr( "Alt+Right" ) );

    m_actToolsBookmarks->setText( tr( "Bookmarks &Manager" ) );
    m_actToolsBookmarks->setShortcut( tr( "Ctrl+M" ) );

    m_actToolsStatistics->setText( tr( "&Statistics" ) );
    m_actToolsStatistics->setShortcut( tr( "F8" ) );

    m_actToolsBookInfo->setText( tr( "Book Information" ) );
    m_actToolsBookInfo->setShortcut( tr( "Ctrl+I" ) );

    m_actToolsFB2Contents->setText( tr( "FictionBook 2 Contents" ) );

    m_actHelpAbout    ->setText( tr( "&About"    ) );
    m_actHelpAboutQt  ->setText( tr( "About &Qt" ) );
    m_actHelpReadme   ->setText( tr( "README"    ) );
    m_actHelpAuthors  ->setText( tr( "AUTHORS"   ) );
    m_actHelpChangelog->setText( tr( "ChangeLog" ) );
    m_actHelpFAQ      ->setText( tr( "FAQ"       ) );
    m_actHelpCopying  ->setText( tr( "COPYING"   ) );

    m_menuFile          ->setTitle( tr( "&File"                   ) );
    m_menuView          ->setTitle( tr( "&View"                   ) );
    m_menuBookmarks     ->setTitle( tr( "&Bookmarks"              ) );
    m_menuResetBookmark ->setTitle( tr( "Set To Current Position" ) );
    m_menuRenameBookmark->setTitle( tr( "Rename Bookmark"         ) );
    m_menuRemoveBookmark->setTitle( tr( "Remove Bookmark"         ) );
    m_menuNav           ->setTitle( tr( "&Navigation"             ) );
//     m_menuFB2Contents   ->setTitle( tr( "FictionBook 2 Contents"  ) );
    m_menuTools         ->setTitle( tr( "Tools"                   ) );
    m_menuHelp          ->setTitle( tr( "&Help"                   ) );

//     if (m_dlgStatistics)
//         m_dlgStatistics->retranslateUI();
//
//     if (m_dlgBookmarks)
//         m_dlgBookmarks->retranslateUI();
//
//     if (m_dlgBookInfo)
//         m_dlgBookInfo->retranslateUI();
//
//     if (m_dlgFB2Contents)
//         m_dlgFB2Contents->retranslateUI();
}

}

#include "ui/guibase.moc"
